package com.piggy.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.piggy.common.core.utils.PageUtils;
import com.piggy.common.core.web.page.PagePlus;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.sys.bo.SysSettingEditBo;
import com.piggy.sys.bo.SysSettingQueryBo;
import com.piggy.sys.domain.SysSetting;
import com.piggy.sys.mapper.SysSettingMapper;
import com.piggy.sys.service.ISysOssService;
import com.piggy.sys.service.ISysSettingService;
import com.piggy.sys.vo.SysSettingVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * 运营系统设置Service业务层处理
 *
 * @author piggy
 * @date 2023-11-13
 */
@Slf4j
@Service
public class SysSettingServiceImpl extends ServiceImpl<SysSettingMapper, SysSetting> implements ISysSettingService {

    @Resource
    private ISysOssService ossService;

    @Override
    public SysSettingVo queryById(Long id) {
        return fillData(getVoById(id, SysSettingVo.class));
    }

    @Override
    public TableDataInfo<SysSettingVo> queryPageList(SysSettingQueryBo bo) {
        PagePlus<SysSetting, SysSettingVo> result = pageVo(PageUtils.buildPagePlus(false), buildQueryWrapper(bo), SysSettingVo.class);
        if (CollUtil.isNotEmpty(result.getRecordsVo())) {
            for (SysSettingVo sysSettingVo : result.getRecordsVo()) {
                fillData(sysSettingVo);
            }
        }
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysSettingVo> queryList(SysSettingQueryBo bo) {
        List<SysSettingVo> voList = listVo(buildQueryWrapper(bo), SysSettingVo.class);
        if (CollUtil.isNotEmpty(voList)) {
            for (SysSettingVo sysSettingVo : voList) {
                fillData(sysSettingVo);
            }
        }
        return voList;
    }

    private LambdaQueryWrapper<SysSetting> buildQueryWrapper(SysSettingQueryBo bo) {
        LambdaQueryWrapper<SysSetting> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, SysSetting::getId, bo.getId());
        lqw.eq(bo.getDeptId() != null, SysSetting::getDeptId, bo.getDeptId());
        lqw.like(StrUtil.isNotBlank(bo.getBossName()), SysSetting::getBossName, bo.getBossName());
        lqw.eq(StrUtil.isNotBlank(bo.getVersion()), SysSetting::getVersion, bo.getVersion());
        lqw.eq(StrUtil.isNotBlank(bo.getFilingNo()), SysSetting::getFilingNo, bo.getFilingNo());
        lqw.eq(StrUtil.isNotBlank(bo.getLogo()), SysSetting::getLogo, bo.getLogo());
        return lqw;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysSettingVo insertByAddBo(SysSettingEditBo bo) {
        SysSetting add = BeanUtil.toBean(bo, SysSetting.class);
        save(add);
        return queryById(add.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysSettingVo updateByEditBo(SysSettingEditBo bo) {
        SysSetting update = BeanUtil.toBean(bo, SysSetting.class);
        updateById(update);
        return queryById(update.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    private SysSettingVo fillData(SysSettingVo vo) {
        if (ObjectUtil.isEmpty(vo)) {
            return vo;
        }
        if (StrUtil.isNotBlank(vo.getLogo())) {
            vo.setLogoUrl(ossService.getSysOssById(Long.parseLong(vo.getLogo())).getData());
        }
        if (StrUtil.isNotBlank(vo.getLogiLogo())) {
            vo.setLogiLogoUrl(ossService.getSysOssById(Long.parseLong(vo.getLogiLogo())).getData());
        }
        if (StrUtil.isNotBlank(vo.getLogiMobile())) {
            vo.setLogiMobileUrl(ossService.getSysOssById(Long.parseLong(vo.getLogiMobile())).getData());
        }
        if (StrUtil.isNotBlank(vo.getLogiPc())) {
            vo.setLogiPcUrl(ossService.getSysOssById(Long.parseLong(vo.getLogiPc())).getData());
        }
        return vo;
    }
}
